package com.software.backend.login.utils;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;



/**
 * @author 王俊坤
 * @version 1.0
 * @date 2022/12/25 11:02
 */
public class SessionContextUtils {

    private static SessionContextUtils instance;

    private HashMap<String, Map<String , Object>> sessionMap;

    private SessionContextUtils() {
        sessionMap = new HashMap<>();
    }

    public static SessionContextUtils getInstance() {
        if (instance == null) {
            instance = new SessionContextUtils();
        }
        return instance;
    }

    public synchronized void addSession(HttpSession session) {
        // 5分钟删除
        sessionMap.values().removeIf(key -> (Long.parseLong(key.get("expireTime").toString()) + 5 * 60 * 1000) <  System.currentTimeMillis());
        if (session != null) {
            Map<String , Object> sessions = new HashMap<>();
            String expireTime = String.valueOf(System.currentTimeMillis());
            sessions.put("expireTime", expireTime);
            sessions.put("session", session);
            String phoneNumber = (String)session.getAttribute("phoneNumber");
            if (phoneNumber != null){
                sessionMap.put(phoneNumber, sessions);
            }
        }
    }

    public synchronized void addSession(HttpSession session, String phoneNumber) {
        // 5分钟删除
        sessionMap.values().removeIf(key -> (Long.parseLong(key.get("expireTime").toString()) + 5 * 60 * 1000) <  System.currentTimeMillis());
        if (session != null) {
            Map<String , Object> sessions = new HashMap<>();
            String expireTime = String.valueOf(System.currentTimeMillis());
            sessions.put("expireTime", expireTime);
            sessions.put("session", session);
            if (phoneNumber != null){
                sessionMap.put(phoneNumber, sessions);
            }
        }
    }

    public synchronized void delSession(HttpSession session) {
        if (session != null) {
            sessionMap.remove(session.getId());
        }
    }

    public synchronized void delSession(String phoneNumber) {
        if (phoneNumber != null) {
            sessionMap.remove(phoneNumber);
        }
    }

    public synchronized String getSession(String sessionName) {
        // 5分钟删除
        sessionMap.values().removeIf(key -> (Long.parseLong(key.get("expireTime").toString()) + 5 * 60 * 1000) <  System.currentTimeMillis());
        if (sessionName == null) {
            return null;
        }
        Map session  = sessionMap.get(sessionName.toLowerCase());
        if (session != null && session.size() > 0 ){
            return (String) ((HttpSession) session.get("session")).getAttribute("authCode");
        }else {
            return null;
        }
    }

    public synchronized boolean canReAddSession(String sessionName) {
        // 5分钟删除
        sessionMap.values().removeIf(key -> (Long.parseLong(key.get("expireTime").toString()) + 5 * 60 * 1000) <  System.currentTimeMillis());
        if (sessionName == null) {
            return false;
        }
        Map session  = sessionMap.get(sessionName.toLowerCase());
        if (session != null && (Long.parseLong(session.get("expireTime").toString()) + 60 * 1000) >=  System.currentTimeMillis()){
            return true;
        }else {
            return false;
        }
    }
}
